<?php
if (!defined('KAIZEKU')) exit(42);
/**
 * WP-iStalker Chrome Min 
 * Stylesheet Handler
 * 
 * PHP 5
 * 
 * @package	WordPress
 * @subpackage	wp-istalker-chrome-min
 * 
 * @category	Template
 * @author	Avice (ChaosKaizer) De'vereux <ck+wp-istalker-min@istalker.net>
 * @author	NH. Noah <noah+wp-istalker-min@kakkoi.net>
 * @copyright 	2007 - 2009 Avice De'vereux, NH. Noah
 * @license 	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License v2 
 * @version 	CVS: $Id: style.php 13 2009-01-11 15:22:40Z chaoskaizer $
 * @since 	0.1
 */

/**
 * wpiStyle
 * @uses $wp_styles WP_styles object (BackPress Scripts enqueue)
 * @since 0.1
 */
final class wpiStyle
{
	/**
	 * Template stylesheets url
	 * 
	 * @access	public
	 * @var		string
	 */			
	public $uri;
		
	/**
	 * WP filter id
	 * @var int
	 */
	public $wp_filter_id;

	/**
	 * Registered stylesheet to be load
	 * 
	 * @access	public
	 * @var		mixed|array
	 */		
	public $css;

	/**
	 * Default theme stylesheets
	 * store all stylesheet handle name
	 * @access	public
	 * @var		mixed|array
	 */		
	public $stylesheets;

	/**
	 * WP hierachical section
	 * store stylesheet for specific sections
	 * @access	public
	 * @var		mixed|array
	 */		
	public $section;

	/**
	 * Loop procedure counter
	 * @access	private
	 * @var		int
	 */	
	private $_flag;

	/**
	 * WordPress base url
	 * 
	 * @see		$wp_styles WP_Style object
	 * @access	public
	 * @var		string
	 */			
	public $base_url;
	
	
	/**
	 * Store array list of wpiStyle error message
	 *	 
	 * @access	public
	 * @var		mixed|array
	 */		
	public $errors;
	

	public function __construct()
	{	global $wp_styles;

		if ( !is_a($wp_styles, 'WP_Styles') ){
			$wp_styles = new WP_Styles();	
		}
		
		foreach(array('css','stylesheets','section','errors') as $meta) $this->$meta = array(); 
		
		$this->base_url = $wp_styles->base_url;
		$this->build = date('Ymd',SV_CURRENT_TIMESTAMP);
					
		$this->_registerStyle($wp_styles);
		
		add_action('wp_head',array($this,'printStyles'),wpiTheme::LAST_PRIORITY+10);
	}
	
	
	/**
	 * wpiStyle::_registerStyle()
	 * 
	 * Register all theme stylesheet	 
	 * 
	 * @uses	$wp_styles WP_Styles object
	 * @access	private
	 * @since	0.1
	 * 
	 * @param	mixed|object $deps WP_styles object 
	 */
	private function _registerStyle($deps)
	{ 
		if ( ($css = wpi_get_dir(WPI_CSS_DIR, wpiTheme::CSS_FILE_REGEX)) != false){
			
			if (has_count($css)){
				
				$this->user_agent = wpi_is_browser();
				
				foreach($css as $file){
					$this->stylesheets[] = $handle = wpiTheme::H_PREFIX.str_rem('.css', $file);
					
					$depencies = array('combine' => true);	
					
					/**
					 * For dependencies with general caching plugins
					 * we exclude the browser stylesheets from
					 * being combine with the main stylsheets 
					 */				
					if ($file == 'browser.css') $depencies = array();					
					
					$deps->add($handle, self::relativeUrl($file, $deps->base_url), $depencies, $this->build, 'all');
				}
				
				unset($css,$file,$handle,$build);
				
				$this->setFlag(1)->defaultStyle();
			}			
		} else {
			$this->errors[] = __(__METHOD__.'() failed',WPI_META);		
		}
	}
	
	
	/**
	 * void wpiStyle::defaultStyle()
	 * register default stylesheets
	 * 
	 * @uses wpi_is_browser() fetch current client browser
	 * @since 0.1
	 */
	public function defaultStyle()
	{	global $wp_styles, $wp_query;
		
		if ($this->_flag <= 0) return;
		
		$stylesheets = array('reset','960','text','images','icecream','style','nav','browser');
		
		$css = array_flip($this->stylesheets);

		/**
		 * Autoload locale stylesheet 
		 * @uses $wp_styles WP_Style object
		 */		
		if ( ($text_direction = $wp_styles->text_direction) != false ){
			if ($this->isReady($text_direction)){
				array_push($stylesheets, $text_direction);
			}	
			unset($text_direction);	
		}
		 		
		/**
		 * filter: wpiFilter::DEFAULT_STYLESHEETS
		 */
		$stylesheets = apply_filters(wpiFilter::DEFAULT_STYLESHEETS, $stylesheets);
		
		if (has_count($stylesheets)){
			foreach($stylesheets as $i){
				$handle = wpiTheme::H_PREFIX.$i;
				
				if (isset($css[$handle])){
					$this->section[wpiSection::K_ALL][] = $i; 	
					wp_enqueue_style($handle);			
				}												
			}
			
			$this->setFlag(2);							
		}
		
		unset($stylesheets,$css);
	}
	
	
	public function isReady($tag){
		$css = array_flip($this->stylesheets);		
		return (isset($css[wpiTheme::H_PREFIX.$tag]));			
	}
	
	
	/**
	 * void public wpiStyle::printStyles()
	 * print queue stylesheets 
	 * 
	 * @uses $wp_styles WP_Style object
	 * @since 0.1
	 * @todo split browser stylesheet for WP related caching plugins
	 */	
	public function printStyles()
	{	global $wp_styles;
	
		if ($this->_flag != 2) return;
		/**
		 * on "_flag" 2 		 
		 * filter style_loader_tag {@see WP_Styles::do_items()} has been null
		 * 
		 * start combine all queue stylesheet		 
		 */
		if (has_count($wp_styles->queue)){
			
			$this->css = $this->section[wpiSection::K_ALL];
		
			// prepare section stylesheet 
			// i.e home, single, page
			$this->_section = is_at();
			
			if ($this->isReady($this->_section)){				
				$this->css[] = $this->_section;				
			}
						
			$this->uri = wpi_get_stylesheet_url($this->css);
			$this->uri = wp_make_link_relative($this->base_url.$this->uri);
			
			/**
			 * Disclaimer: Take notes, the LINK "title" attributes is important,
			 * Do not modified it!. The stylesheet will not render correctly
			 * if the title is different than "wpiTheme::UID"
			 * @see wpiTemplate::httHeader()
			 */
			t('link','',array('rel'=>'stylesheet','href'=> $this->uri,'type'=>'text/css','title'=> wpiTheme::UID,'media'=>'all'));
			
			$this->setFlag(3);
			
			//wpi_dump($this);		
		}		
	}
		
	
	/**
	 * public wpiStyle::setFlag()
	 * set conditional procedure flag number 
	 * for debugging purpose
	 * @param mixed|int	$int Number
	 * @return pointer current object
	 */
	public function setFlag($int)
	{
		$this->_flag = (int) $int;	
		return $this;
	}
	

	/**
	 * void public wpiStyle::getFlag()
	 * get conditional procedure flag number 
	 */
	public function getFlag()
	{	
		return $this->_flag;
	}
		
	
	public function dump()
	{	global $wp_styles;
		wpi_dump($wp_styles);
	}
	
	
	/**
	 * static wpiStyle::relativeUrl()
	 * Make link relative to WP_CONTENT_URL
	 * 
	 * @uses	$wp_styles	WP_Style object
	 * @since	0.1
	 * @param	string $filename The css filename
	 * @param	string $base_url Wordpress URL  
	 * @return	string Relative css URL from wp-content URL
	 * 
	 */
	public static function relativeUrl($filename, $base_url=false)
	{
		if (!$base_url){
			global $wp_styles;
			$base_url = $wp_styles->base_url;		
		}
		
		return str_rem($base_url, WPI_THEME_URL.$filename);		
	}

	
	public function __destruct(){ unset($this); }
	
	
	private function __clone(){}	
}				
?>